package com.qing.library_manager.service.Impl.borrowImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.library_manager.dao.book.bookMapper;
import com.qing.library_manager.dao.borrow.borrowMapper;
import com.qing.library_manager.dao.readers.readersMapper;
import com.qing.library_manager.domain.dto.BorrowAllDTO;
import com.qing.library_manager.domain.dto.BorrowDTO;
import com.qing.library_manager.domain.dto.ReadersDTO;
import com.qing.library_manager.domain.entity.Borrow;
import com.qing.library_manager.domain.query.*;
import com.qing.library_manager.service.manager.borrow.borrowService;
import com.qing.library_manager.common.util.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class borrowServiceImpl extends ServiceImpl<borrowMapper, Borrow> implements borrowService {

    private final borrowMapper borrowMapper;
    private final bookMapper bookMapper;
    private final readersMapper readersMapper;

    public borrowServiceImpl(com.qing.library_manager.dao.borrow.borrowMapper borrowMapper,
                             com.qing.library_manager.dao.book.bookMapper bookMapper,
                             com.qing.library_manager.dao.readers.readersMapper readersMapper) {
        this.borrowMapper = borrowMapper;
        this.bookMapper = bookMapper;
        this.readersMapper = readersMapper;
    }


    /**
     * 增加借阅信息业务
     *
     * @param borrowQuery
     * @return
     */
    @Override
    public ResponseResult addBorrow(BorrowQuery borrowQuery) {
        int i = borrowMapper.queryBorrowNum(
                new BorrowQuery(borrowQuery.getReadersName(), borrowQuery.getBookName(), borrowQuery.getBookAuthorName()));
        if (i > 0) {
            return ResponseResult.FAILED();
        }
        /**
         * 补全数据
         */
        Borrow borrow = new Borrow();
        borrow.setBookId(bookMapper.queryBookIdCreate(new BookQueryByName(borrowQuery.getBookName(), borrowQuery.getBookAuthorName())));
        borrow.setReadersId(readersMapper.queryReadersIdCreate(borrowQuery.getReadersName()));
        borrow.setBorrowReturn("未归还");
        borrow.setBookBorrow(LocalDateTime.now());
        /**
         * 设置归还日期为三个月以后
         */
        borrow.setBookReturn(LocalDateTime.now().plusMonths(3l));
        borrow.setBorrowInfoStatus("1");
        int flag = borrowMapper.insert(borrow);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 删除借阅信息
     *
     * @param borrowShowId
     * @return
     */
    @Override
    public ResponseResult deleteBorrow(int borrowShowId) {
        int flag = borrowMapper.deleteBorrow(borrowShowId);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 查询读者借阅信息
     *
     * @param readersName
     * @return
     */
    @Override
    public ResponseResult queryBorrowByInit(String readersName) {
        List<BorrowAllDTO> borrowAllDTOS = borrowMapper.queryBorrowByInit(readersName);
        return borrowAllDTOS != null ? ResponseResult.SUCCESS(borrowAllDTOS) : ResponseResult.FAILED();
    }

    /**
     * 归还图书
     *
     * @param borrowShowId
     * @return
     */
    @Override
    public ResponseResult borrowReturn(int borrowShowId) {
        int i = borrowMapper.borrowReturn(borrowShowId);
        return i > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 根据特定条件来搜索
     *
     * @param readersName
     * @param bookName
     * @return
     */
    @Override
    public ResponseResult queryBorrowByName(String readersName, String bookName) {
        List<BorrowAllDTO> borrowAllDTOS = borrowMapper.queryBorrowByName(readersName, bookName);
        return borrowAllDTOS != null ? ResponseResult.SUCCESS(borrowAllDTOS) : ResponseResult.FAILED();
    }

    /**
     * 更新借阅信息
     *
     * @param borrowUpdateQuery
     * @return
     */
    @Override
    public ResponseResult updateBorrowInfo(BorrowUpdateQuery borrowUpdateQuery) {
        int flag = borrowMapper.updateBorrowInfo(borrowUpdateQuery);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 分页查询数据
     *
     * @param pageNum
     * @param pageSize
     * @param borrowByPageQuery
     * @return
     */
    @Override
    public ResponseResult queryBorrowByPage(int pageNum, int pageSize, BorrowByPageQuery borrowByPageQuery) {
        Page<BorrowAllDTO> page = new Page<>(pageNum, pageSize);
        IPage<BorrowAllDTO> borrowAllDTOIPage = borrowMapper.queryBorrowByPage(page, borrowByPageQuery);
        return borrowAllDTOIPage != null ? ResponseResult.SUCCESS(borrowAllDTOIPage) : ResponseResult.FAILED();
    }

    /**
     * 根据id查询数据
     *
     * @param borrowShowId
     * @return
     */
    @Override
    public ResponseResult queryBorrowById(int borrowShowId) {
        BorrowAllDTO borrowAllDTO = borrowMapper.queryBorrowById(borrowShowId);
        return borrowAllDTO != null ? ResponseResult.SUCCESS(borrowAllDTO) : ResponseResult.FAILED();
    }

    /**
     * 获取全部借阅信息
     *
     * @return
     */
    @Override
    public ResponseResult queryAllBorrow() {
        List<BorrowAllDTO> borrowAllDTOS = borrowMapper.queryAllBorrow();
        return borrowAllDTOS != null ? ResponseResult.SUCCESS(borrowAllDTOS) : ResponseResult.FAILED();
    }
}
